<template>
  <div class="sideBar">
    <div
      class="projectTitle"
      :class="$store.state.isCollapse?'menu-bar-collapse-width':'menu-bar-width'"
      :style="'background-color:#2d2e37'"
    >
      <img v-if="$store.state.isCollapse" class="logo" src="@/assets/logo.png" />
      <div v-else class="appName">{{appName}}</div>
    </div>
    <el-menu
      v-if="refreshMenu"
      style="height: 100%"
      class="el-menu-vertical-demo"
      background-color="#2d2e37"
      text-color="#fff"
      active-text-color="#ffd04b"
      :collapse="$store.state.isCollapse"
      :unique-opened="true"
      :default-active="$route.path || ''"
      :mode="isMobileFlag ? 'horizontal' : 'vertical'"
      :menu-trigger="isMobileFlag ? 'click': 'hover'"
    >
      <!-- <el-menu-item index="/index" @click="turnMenu({url: '/index', name: '首页'})">
        <i class="el-icon-s-home"></i>
        <span slot="title">首页</span>
      </el-menu-item>-->
      <el-submenu v-for="(item,index) in menuList" :key="index" :index="String(index+1)">
        <template slot="title">
          <i :class="item.icon"></i>
          <span slot="title">{{item.title}}</span>
        </template>
        <el-menu-item
          :index="menu.url"
          v-for="(menu,menuIndex) in item.menus"
          :key="menuIndex"
          @click="turnMenu(menu)"
        >{{menu.name}}</el-menu-item>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
import { userMenu, adminMenu, orderMenu, sellerMenu, tradeForeignMenu } from "@/lib/menu";
export default {
  name: "sideBar",
  components: {},
  data() {
    return {
      isMobileFlag: this.$utils.isMobile(),
      themeColor: this.$store.state.themeColor, //主题颜色
      appName: this.$store.state.appName,
      menuList: [],
      userMenu: userMenu,
      adminMenu: adminMenu,
      orderMenu: orderMenu,
      sellerMenu: sellerMenu,
      tradeForeignMenu: tradeForeignMenu,
      refreshMenu: true // 用于强制重新渲染菜单
    };
  },
  methods: {
    turnMenu(item) {
      // 点击菜单跳转
      this.$router.push({ path: item.url });
    },
    changeCollapse() {
      //改变导航栏状态
      this.$store.commit("changeCollapse");
    },
    getUserInfo() {
      this.$api.user.GetUserInfo().then(res => {
        this.$store.commit("setUserInfo", res.data.userInfo);
      });
      // 调试调试！！！
      this.menuList = this.menuList.concat(
        this.adminMenu,
        this.orderMenu,
        this.sellerMenu,
        this.userMenu,
        this.tradeForeignMenu,
      );
    }
  },
  created() {
    this.getUserInfo();
  }
};
</script>

<style lang="scss" scoped>
.sideBar {
  position: absolute;
  height: calc(100% - 65px);
  top: 0;
  left: 0;
  z-index: 1020;
  .projectTitle {
    transition: 0.3s;
    height: 64px;
    text-align: center;
    line-height: 64px;
    border-right: solid 1px #e6e6e6;
    .appName {
      color: white;
      font-size: 25px;
    }
    .logo {
      margin: 12px;
      width: 40px;
      height: 40px;
    }
  }
  .menu-bar-width {
    width: 200px;
  }
  .menu-bar-collapse-width {
    width: 64px;
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
    .el-submenu {
      i {
        margin-right: 10px;
      }
    }
  }
}
</style>